package com.goods.business.service.imp;


import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.service.ManageService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class ManageServiceImpl implements ManageService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;


    @Override
    public PageVO<ProductCategoryTreeNodeVO> categoryTree(Integer pageNum,Integer pageSize) {
        //PageHelper.startPage(pageNum,pageSize);
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = getProductCategoryTreeNodeVOS();
        List<ProductCategoryTreeNodeVO> build = CategoryTreeBuilder.build(productCategoryTreeNodeVOS);
        if (pageNum == null && pageSize==null){
            return new PageVO<>(build.size(),build);
        }
        PageInfo<ProductCategoryTreeNodeVO> pageInfo = new PageInfo<>(build);
        List<ProductCategoryTreeNodeVO> page = ListPageUtils.page(build, pageSize, pageNum);
        return new PageVO<>(pageInfo.getTotal(),page);
    }

    private List<ProductCategoryTreeNodeVO> getProductCategoryTreeNodeVOS() {
        List<ProductCategory> productCategories = productCategoryMapper.selectAll();
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(productCategories)){
            productCategoryTreeNodeVOS = productCategories.stream().map(
                    productCategory -> {
                        ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
                        BeanUtils.copyProperties(productCategory, productCategoryTreeNodeVO);
                        return productCategoryTreeNodeVO;
                    }
            ).collect(Collectors.toList());
        }
        return productCategoryTreeNodeVOS;
    }

    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = getProductCategoryTreeNodeVOS();
        List<ProductCategoryTreeNodeVO> products = CategoryTreeBuilder.buildParent(productCategoryTreeNodeVOS);

        return products;
    }

    @Override
    public void add(ProductCategory productCategory) {
        productCategory.setCreateTime(new Date());
        productCategory.setModifiedTime(new Date());
        productCategoryMapper.insert(productCategory);
    }

    @Override
    public boolean delete(Long id) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setPid(id);
        ProductCategory productCategory1 = productCategoryMapper.selectOne(productCategory);
        if (productCategory1!=null) {
            return false;
        } else {
            ProductCategory productCategory2 = new ProductCategory();
            productCategory2.setId(id);
            return productCategoryMapper.delete(productCategory2) > 0;
        }
    }

    @Override
    public ProductCategoryTreeNodeVO edit(Long id) {
        ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
        ProductCategory productCategory = new ProductCategory();
        productCategory.setId(id);
        ProductCategory productCategory1 = productCategoryMapper.selectOne(productCategory);
        BeanUtils.copyProperties(productCategory1,productCategoryTreeNodeVO);
        return productCategoryTreeNodeVO;
    }

    @Override
    public void update(Long id, ProductCategoryTreeNodeVO productCategory) {
        ProductCategory productCategory1 = new ProductCategory();
        BeanUtils.copyProperties(productCategory,productCategory1);
        productCategory1.setId(id);
        productCategoryMapper.updateByPrimaryKey(productCategory1);
    }

}
